package com.library.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.dao.BookDAO;
import com.library.entity.Book;
import com.library.entity.User;

/**
 * 用户图书浏览Servlet
 */
@WebServlet("/user/book")
public class UserBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String action = request.getParameter("action");
        
        if (action == null) {
            // 默认显示所有图书
            showAllBooks(request, response);
        } else if (action.equals("view")) {
            // 查看图书详情
            viewBook(request, response);
        } else if (action.equals("search")) {
            // 搜索图书
            searchBooks(request, response);
        } else if (action.equals("category")) {
            // 按分类查看图书
            viewBooksByCategory(request, response);
        } else {
            // 默认显示所有图书
            showAllBooks(request, response);
        }
    }
    
    // 显示所有图书
    private void showAllBooks(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        BookDAO bookDAO = new BookDAO();
        List<Book> books = bookDAO.getAllBooks();
        
        request.setAttribute("books", books);
        request.getRequestDispatcher("/user/books.jsp").forward(request, response);
    }
    
    // 查看图书详情
    private void viewBook(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        
        BookDAO bookDAO = new BookDAO();
        Book book = bookDAO.getBookById(id);
        
        // 检查用户是否已登录
        HttpSession session = request.getSession(false);
        boolean isLoggedIn = session != null && session.getAttribute("user") != null;
        
        request.setAttribute("book", book);
        request.setAttribute("isLoggedIn", isLoggedIn);
        request.getRequestDispatcher("/user/book_detail.jsp").forward(request, response);
    }
    
    // 搜索图书
    private void searchBooks(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        
        BookDAO bookDAO = new BookDAO();
        List<Book> books = bookDAO.searchBooksByTitle(keyword);
        
        request.setAttribute("books", books);
        request.setAttribute("keyword", keyword);
        request.getRequestDispatcher("/user/books.jsp").forward(request, response);
    }
    
    // 按分类查看图书
    private void viewBooksByCategory(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String category = request.getParameter("category");
        
        BookDAO bookDAO = new BookDAO();
        List<Book> books = bookDAO.getBooksByCategory(category);
        
        request.setAttribute("books", books);
        request.setAttribute("category", category);
        request.getRequestDispatcher("/user/books.jsp").forward(request, response);
    }
}
